﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DualTemplate.aspx.cs" Inherits="CRMWeb.Pages.Admin.DualTemplate" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Dual Template</title>
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/icon.css" />
    <link rel="stylesheet" type="text/css" href="/Css/CrmStyle.css" />
    <script type="text/javascript" src="/Scripts/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/EasyUI/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/tiny_mce/tiny_mce.js"></script>
    <script type="text/javascript" src="/Scripts/common.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div id="pnlDualTemplate" title="Dual Template" class="easyui-panel" style="width: auto; height: auto">
            <div class="fitem" id="toolbar" style="margin-left: 10px">
                <a id="lnknewDualTemplate" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newDualTemplate()">New</a>

                <a id="lnkdeleteDualTemplate" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="deleteDualTemplate()">Delete</a>
            </div>
            <input id="hdnDualTemplateID" type="hidden" />
            <input id="hdnEmailSub" type="hidden" />
            <input id="hdnSMSCategoryID" type="hidden" />
            <input id="hdnEmailCategoryID" type="hidden" />
            <div class="fitem">
                <label style="width: 100px; margin-left: 10px; text-align: left">Dual Template :</label>
                <span id="spncmbDualTemplate">
                    <select class="easyui-combotree" id="cmbDualTemplate" name="Field" style="width: 450px"
                        data-options="url:'/WebServices/DualTemplate.ashx?method=DisplayDualTemplateNode',required:true,onSelect:selectDualTemplate">
                    </select>

                </span>
                <span id="spntxtDualTemplate">
                    <input id="txtNewDualTemplate" class="easyui-validatebox" required="true" />
                </span>
            </div>

            <div class="fitem">
                <label style="width: 100px; margin-left: 10px; text-align: left">E-Mail Template :</label>
                <span id="spncmbEmailTemplate">
                    <select class="easyui-combotree" id="cmbEmailTemplate" style="width: 450px;"
                        data-options="url:'/WebServices/EmailTemplate.ashx?method=DisplayTemplateNode',required:true, onSelect:selectEmailTemplate">
                    </select>

                </span>
            </div>
            <div class="fitem">
                <label style="width: 100px; margin-left: 10px; text-align: left">SMS Template :</label>
                <span id="spncmbSmsTemplate">
                    <select class="easyui-combotree" id="cmbSmsTemplate" style="width: 450px;"
                        data-options="url:'/WebServices/SMSTemplate.ashx?method=DisplayTemplateNode',required:true,onSelect:selectSMSTemplate">
                    </select>

                </span>
            </div>
            <div class="fitem">
                <span id="spnCategory" style="display: none">
                    <label style="width: 100px; margin-left: 10px; text-align: left">Category: </label>
                    <select class="easyui-combotree" id="cmbCategory" style="width: 450px;"
                        data-options="url:'/WebServices/DualTemplate.ashx?method=DisplayDualCategoryList',required:true">
                    </select>
                </span>
            </div>
            <div class="fitem">
                <label style="width: 100px; margin-left: 10px; text-align: left">Subject: </label>
                <input id="txtDualTempSub" name="txt_sub" type="text" style="width: 300px;" class="easyui-validatebox" required="true" />

            </div>

            <div class="fitem">
                <label style="width: 150px; margin-left: 10px">E-Mail Template Format: </label>
            </div>
            <div class="fitem" style="padding: 10px;">

            <%--<div data-options="region:'center'" style="padding: 10px;height:100%"  class="emailbox">--%>
                <textarea id="txtEmailFormat" name="MailDescription" class="easyui-validatebox" style="width: 100%; height: 410px"></textarea>
            </div>
            <div class="fitem">
                <label style="width: 150px; margin-left: 10px">SMS Template Format : </label>
            </div>
            <div data-options="region:'center'" style="padding: 10px; height:11%" class="smsbox">
                <textarea id="txtSmsFormat" name="SMSDescription" class="easyui-validatebox" style="width: 100%; height: 85%"></textarea>
            </div>
            <div style="text-align: right; width: auto; margin-right: 5%">
                <a id="lnkcancelDualTemplate" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="cancelDualTemplate()">Cancel </a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="SaveDualTemplate()">Save </a>
            </div>

        </div>
    </form>
</body>
<script type="text/javascript">
    $(document).ready(function () {
        $('#cmbDualTemplate').combotree({
            onClick: function (node) {
                if (!$('#cmbDualTemplate').tree('isLeaf', node.target)) {
                    $('#cmbDualTemplate').combotree('setValues', '');
                    var t = $('#cmbDualTemplate').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#cmbDualTemplate').combotree('panel');
                    p.panel('open');
                }
            }
        });
        $('#cmbEmailTemplate').combotree({
            onClick: function (node) {
                if (!$('#cmbEmailTemplate').tree('isLeaf', node.target)) {
                    $('#cmbEmailTemplate').combotree('setValues', '');
                    var t = $('#cmbEmailTemplate').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#cmbEmailTemplate').combotree('panel');
                    p.panel('open');
                }
            }
        });
        $('#cmbSmsTemplate').combotree({
            onClick: function (node) {
                if (!$('#cmbSmsTemplate').tree('isLeaf', node.target)) {
                    $('#cmbSmsTemplate').combotree('setValues', '');
                    var t = $('#cmbSmsTemplate').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#cmbSmsTemplate').combotree('panel');
                    p.panel('open');
                }
            }
        });
        $('#cmbCategory').combotree({
            onClick: function (node) {
                if (!$('#cmbCategory').tree('isLeaf', node.target)) {
                    $('#cmbCategory').combotree('setValues', '');
                    var t = $('#cmbCategory').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#cmbCategory').combotree('panel');
                    p.panel('open');
                }
            }
        });
        $('#spntxtDualTemplate').hide();
        $('#txtNewDualTemplate').val('');
        $('#pnlDualTemplate').panel('resize', {
            width: window.innerWidth,
            height: window.innerHeight
        });
        $('#cmbEmailTemplate').combobox('textbox').bind('click', function () {
            $('#cmbEmailTemplate').combobox('showPanel');
        });
        $('#cmbSmsTemplate').combobox('textbox').bind('click', function () {
            $('#cmbSmsTemplate').combobox('showPanel');
        });
        $('#cmbDualTemplate').combobox('textbox').bind('click', function () {
            $('#cmbDualTemplate').combobox('showPanel');
        });
        $('#cmbCategory').combobox('textbox').bind('click', function () {
            $('#cmbCategory').combobox('showPanel');
        });

        $('#lnkdeleteDualTemplate').hide();
        $('#lnkcancelDualTemplate').hide();
    });
    window.onresize = function () {
        $('#pnlDualTemplate').panel('resize', {
            width: window.innerWidth,
            height: window.innerHeight
        });
    }
    function selectEmailTemplate(node) {
        var currentTemplateId = node.id;
        if (currentTemplateId == -1) {
            $.messager.alert('Morgan CRM', 'Please select Template Item ... !', 'warning');
            $('#cmbEmailTemplate').combotree('setValue', '');
        }
        else {
            $.ajax({
                url: "/WebServices/EmailTemplate.ashx?method=GetEmailTemplateContentById",
                contentType: "application/json; charset=utf-8",
                data: { EmailTemplateId: currentTemplateId },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {
                        tinymce.get('txtEmailFormat').setContent(decodeURIComponent(result.TemplateFormat));
                        $('#hdnEmailSub').val(result.emailsubject);
                        $('#hdnEmailCategoryID').val(result.CategoryId);
                    });
                }
            });
        }
    }

    function selectSMSTemplate(node) {

        var currentTemplateId = node.id;
        if (currentTemplateId == -1) {
            $.messager.alert('Morgan CRM', 'Please select Template Item ... !', 'warning');
            $('#cmbSmsTemplate').combotree('setValue', '');
        }
        else {
            $.ajax({
                url: "/WebServices/SMSTemplate.ashx?method=GetSMSTemplateContentById",
                contentType: "application/json; charset=utf-8",
                data: { smsTemplateId: currentTemplateId },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {
                        $('#txtSmsFormat').val(result.TemplateFormat);
                        $('#hdnSMSCategoryID').val(result.CategoryId);
                    });
                }
            });
        }
    }

    function selectDualTemplate(node) {
        var dualTemplateId = node.id;
        if (dualTemplateId == -1) {
            $('#cmbDualTemplate').combotree('setValue', '');
            $.messager.alert('Morgan CRM', 'Please select Template Item ... !', 'warning');
        }
        else {
            $('#lnkdeleteDualTemplate').show();
            $.ajax({
                url: "/WebServices/DualTemplate.ashx?method=GetDualTemplateContentById",
                contentType: "application/json; charset=utf-8",
                data: { DualTemplateId: dualTemplateId },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {

                        $('#cmbCategory').combotree('setValue', result.CategoryId);
                        $('#hdnDualTemplateID').val(dualTemplateId);
                        $('#cmbEmailTemplate').combotree('setValue', result.EmailTemplateID);
                        $('#cmbSmsTemplate').combotree('setValue', result.SmsTemplateID);
                        $('#txtDualTempSub').val(result.DualSubject);
                    });
                }
            });
        }
    }

    function newDualTemplate() {
        $('#pnlDualTemplate').panel('setTitle', 'Create New Dual Template');

        $('#spncmbDualTemplate').hide();
        $('#spntxtDualTemplate').show();
        $('#spnCategory').show();
        $('#lnknewDualTemplate').hide();
        $('#lnkdeleteDualTemplate').hide();
        $('#lnkcancelDualTemplate').show();
        resetForm();
    }
    function resetForm() {
        $('#cmbDualTemplate').combotree('reload');
        $('#cmbEmailTemplate').combotree('reload');
        $('#cmbSmsTemplate').combotree('reload');
        $('#cmbDualTemplate').combotree('setValue', '');
        $('#cmbEmailTemplate').combotree('setValue', '');
        $('#cmbSmsTemplate').combotree('setValue', '');

        $('#txtNewDualTemplate').val('');
        $('#txtDualTempSub').val('');
        tinymce.get('txtEmailFormat').setContent('');
        $('#txtSmsFormat').val('');
        $('#hdnDualTemplateID').val(0);
    }
    function cancelDualTemplate() {
        $('#pnlDualTemplate').panel('setTitle', ' Dual Template');
        $('#spncmbDualTemplate').show();
        $('#spntxtDualTemplate').hide();
        $('#spnCategory').hide();
        $('#lnknewDualTemplate').show();
        $('#lnkcancelDualTemplate').hide();
        $('#lnkdeleteDualTemplate').hide();
        resetForm();
    }
    tinyMCE.init({
        mode: "exact",
        elements: "txtEmailFormat",
        theme: "advanced",
        plugins: "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
        theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2: "bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,help,code,|,forecolor,backcolor",
        setup: function (ed) {
            ed.onInit.add(function (ed) {
                ed.getDoc().body.style.fontSize = '13pt';
            });
        }
    });
    function SaveDualTemplate() {
        var SmsTemplateFormat = $('#txtSmsFormat').val();
        var EmailTemplateFormat = encodeURIComponent(tinyMCE.get('txtEmailFormat').getContent());
        var TemplateId = $('#hdnDualTemplateID').val();
        var TemplateName = '';
        if (TemplateId != 0)
            TemplateName = $('#cmbDualTemplate').combobox('getText');
        else
            TemplateName = $('#txtNewDualTemplate').val();


        var temail = $('#cmbEmailTemplate').combotree('tree');
        var emailNode = temail.tree('getSelected');

        var tsms = $('#cmbSmsTemplate').combotree('tree');
        var smsNode = tsms.tree('getSelected');

        var tr = $('#cmbCategory').combotree('tree');
        var Categorynode = tr.tree('getSelected');

        $.ajax({
            url: '/WebServices/DualTemplate.ashx?method=saveDualTemplate',
            data: { DualTemplateID: TemplateId, DualSubject: $('#txtDualTempSub').val(), DualTemplate: TemplateName, SmsTemplateID: smsNode.id, EmailTemplateID: emailNode.id, CategoryID: Categorynode.id },
            success: function (data) {
                if (TemplateId != 0) {

                    if (smsNode.id != '') {
                        if (SmsTemplateFormat != '') {
                            $.ajax({
                                url: '/WebServices/SMSTemplate.ashx',
                                type: 'POST',
                                data: {
                                    method: 'SMSTemplateUpdate',
                                    args: { SMSTemplateID: smsNode.id, TemplateFormat: SmsTemplateFormat, CategoryID:  $('#hdnSMSCategoryID').val() }
                                }
                            });
                        }
                    }

                    if (emailNode.id != '') {
                        if (EmailTemplateFormat != '') {
                            $.ajax({
                                url: '/WebServices/EmailTemplate.ashx',
                                type: 'POST',
                                data: {
                                    method: 'EmailTemplateUpdate',
                                    args: { EmailId: emailNode.id, TemplateFormat: EmailTemplateFormat, emailSubject: $('#hdnEmailSub').val(), CategoryID:  $('#hdnEmailCategoryID').val() }
                                }
                            });
                        }
                    }

                    $.messager.alert('Morgan CRM', 'Dual Template Updated');
                    cancelDualTemplate();
                }
                else {

                    if (smsNode.id != '') {
                        if (SmsTemplateFormat != '') {
                            $.ajax({
                                url: '/WebServices/SMSTemplate.ashx',
                                type: 'POST',
                                data: {
                                    method: 'SMSTemplateUpdate',
                                    args: { SMSTemplateID: smsNode.id, TemplateFormat: SmsTemplateFormat, CategoryID: $('#hdnSMSCategoryID').val() }
                                }
                            });
                        }
                    }

                    if (emailNode.id != '') {
                        if (EmailTemplateFormat != '') {
                            $.ajax({
                                url: '/WebServices/EmailTemplate.ashx',
                                type: 'POST',
                                data: {
                                    method: 'EmailTemplateUpdate',
                                    args: { EmailId: emailNode.id, TemplateFormat: EmailTemplateFormat, emailSubject: $('#hdnEmailSub').val(), CategoryID: $('#hdnEmailCategoryID').val() }
                                }
                            });
                        }
                    }

                    $.messager.alert('Morgan CRM', 'New Dual Template Created');
                    cancelDualTemplate();
                }
            }
        });
    }
    function deleteDualTemplate() {
        var TemplateId = $('#hdnDualTemplateID').val();
        $.ajax({
            url: '/WebServices/DualTemplate.ashx?method=deleteDualTemplate',
            data: { DualTemplateID: TemplateId },
            success: function (data) {
                $.messager.alert('Morgan CRM', 'Dual Template Deleted');
                cancelDualTemplate();
            }
        });
    }

</script>
</html>
